package PTA_JAVA.Exception.Exception04;

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }
        int m = sc.nextInt();
        for (int i = 0; i < m; i++) {
            int begin = sc.nextInt();
            int end = sc.nextInt();
            try{
                ArrayUtil.findMax(arr,begin,end);
            }catch (IllegalArgumentException e){
                System.out.println(e);
            }
        }
    }
}
class ArrayUtil{

    public static void findMax(int[] arr, int begin, int end) {
        if(begin>=end){
            throw new IllegalArgumentException("begin:"+begin+" >= end:"+end);
        }else if(begin<0){
            throw new IllegalArgumentException("begin:"+begin+" < 0");
        }else if(end>arr.length){
            throw new IllegalArgumentException("end:"+end+" > arr.length");
        }else{
            int max=0;
            for(int i=begin;i<end;i++){
                if(arr[i]>max){
                    max = arr[i];
                }
            }
            System.out.println(max);
        }
    }
}
